package com.itheima.test03;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

/*
## 题目3（加强训练）
需求：编写程序，使用UDP协议的发送端给接收端发送一句话：“服务端你好啊！”；然后接收端给发送端返回一句话，内容随意。
 */
public class TestUdpService {
    public static void main(String[] args) throws IOException {
        //接
        DatagramSocket socket = new DatagramSocket(11111);
        byte[] bytes = new byte[1024];
        DatagramPacket packet = new DatagramPacket(bytes, bytes.length);
        socket.receive(packet);
        byte[] data = packet.getData();
        System.out.println(new String(data,0,packet.getLength()));

        //发
        String s = "我是服务器";
        socket.send(new DatagramPacket(s.getBytes(),s.getBytes().length,InetAddress.getByName("127.0.0.1"),22222));

        socket.close();
    }
}
